<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'order';
$return=array();
//声明页面上方tab按钮
$tabs=array(
	array('op'=>'order','title'=>'已获取订单列表'),
	array('op'=>'update2','title'=>'同步有赞订单数据'),
);
set_html('tabs',$tabs);
//声明列表页
if($_RQ['op']=='order'){
	//初始化返回前端的筛选条件数组
	$return['params']=array();
	//整理列表数据查询条件
	$params=array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_yzwd');
	if($_RQ['ordersn']){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	$psize=15;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'table'=>'jy_weishop_order',
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	//执行分页查询
	$list=pdo_getlist($parameter,$total);
	//将查询到的结果数组放入$return以备传回前端
	$return['list']=$list;
	//整理分页
	$return['totalpage'] =intval(ceil($total/$psize));
	$return['params']['page'] =$pindex;
	//声明列表页样式数组
	$htmls=array(
		'type'=>'list',
		'export'=>'export',//声明导出动作op，如无需导出功能，可不声明该字段
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			//列表展示列
			array('type'=>'string','name'=>'ordersn','title'=>'订单号',),
			array('type'=>'string','name'=>'price','title'=>'订单价格',),
			array('type'=>'string','name'=>'status','title'=>'订单状态',),
			array('type'=>'string','name'=>'ispay','title'=>'支付状态',),
			array('type'=>'string','name'=>'remake','title'=>'备注',),
		)),
		'search'=>array(
			//列表查询表单
			array('type'=>'string','name'=>'title','title'=>'商品名称','width'=>'50'),
		),
	);
	$return['htmls']=$htmls;
	//把数据返回前端
	exi($return);
}
//导出动作
if($_RQ['op']=='export'){
	//整理筛选条件
	if($_RQ['ordersn']){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
	}
	//声明需要查询哪些字段，为空表示获取所有
	$fields=array();
	$parameter=array(
		'params'=>$params,
		'fields'=>$fields,
		'orderby'=>'id desc',
	);
	//执行分页查询
	$list=$this->get_list('jy_weishop_order',$parameter);
	if($list){
		foreach($list as &$item){
			//整理从数据库获取到的数据
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	//声明表头和对应字段
	$header=array(
		'title'=>'商品名称',
		'price'=>'商品价格',
		'quantity'=>'商品库存',
	);
	//执行导出
	model('core/tools')->export2excel($header,$list,'订单列表');
}
// 商品数据同步
if($_RQ['op']=='update2'){
	$data=$this->model('func')->getOrders();
	if(!empty($data)){
		foreach($data as $value){			
			$orders =array(
					'ordersn'=>trim($value['source_info']['book_key']),
					'price'=>trim($value['pay_info']['payment']),
					'goodsprice'=>trim($value['orders']['price']),
					'discountprice'=>$value['orders']['total_fee'],
					'status'=>$value['order_info']['status'],
					'buytype'=>$value['order_info']['type'],
					'ispay'=>$value['order_info']['pay_type'],
					'pay_type'=>$value['orders']['buyer_messages'],
					'createtime'=>$value['order_extra']['created'],
					'refundstatus'=>$value['order_info']['refund_state'],
					'paytime '=>$value['order_info']['pay_time'],
					
			);
			$orders_id=$this->pdo_getcolumn('orders',array('item_id'=>$value['item_id']),'orders_id');
			if($orders_id){
				pdo_update('jy_weishop_order',$orders,array('id'=>$orders_id,'uniacid'=>$_SESSION['uniacid']));
			}else{
				$orders['uniacid']=$_SESSION['uniacid'];
				$orders['plugin_name']='jy_yzwd';
				pdo_insert('jy_weishop_order',$orders);
				$orders_id=pdo_insertid();
				$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'item_id'=>$value['item_id'],
					'orders_id'=>$orders_id,
				);
				$this->pdo_insert('orders',$insert);
			}
		}
		exi('数据同步成功！','','list');
	}
	exi('数据同步失败，未获取到有赞商城的订单数据','','list');
}